#include"stdio.h"
#include<cmath>
#include<cstring>//1258Mystrcat(str1,str2);
int flag[20]={10,91,739,5275,32491,168571,712891,2345851,5611771};
int b[10][10]={
    {1},
    {9,1},
    {72,8,1},
    {504,56,7,1},
    {3024,336,42,6,1},
    {15120,1680,210,30,5,1},
    {60480,6720,840,120,20,4,1},
    {181440,20160,2520,360,60,12,3,1}, 
    {362880,40320,5040,5040,720,120,24,6,2,1},
    {362880,40320,5040,720,120,24,6,2,1,1}
};
int main()
{
    int i,n;
    int w;
    while(scanf("%d",&n)!=EOF){
        char a[11]={0};
        char c[10]={0,1,2,3,4,5,6,7,8,9};
        for(i=0;n>flag[i]&&i<9;i++);
        w=i+1;
        if(i>0)
        n-=flag[i-1]+1;
        for(i=0;i<w;i++){
            a[i]=n/b[w-1][i];
            n%=b[w-1][i];
            
        }
        if(w==1)
        printf("%d\n",a[0]-1);
        else{
            i=a[0]+1;
            a[0]=c[a[0]+1]+'0';
            c[i]=-1;
            for(i=1;i<w;i++){
                int j;
                for(j=0;j<10;j++)
                if(a[i]==0&&c[j]!=-1){
                    a[i]=c[j]+'0';
                    c[j]=-1;
                    break;
                }else if(c[j]!=-1)
                a[i]--;
            }
            printf("%s\n",a);
        }
        
    }
    return 0;
}

